package com.supreme.commonboot.condition;

import com.supreme.commonboot.annotation.ControllerAopMatch;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;
import java.util.Optional;

public class ControllerAopCondition implements Condition {

    private static final String controllerAopEnable = "supreme-server.controllerAopEnable";

    /**
     * 参数说明
     * @param conditionContext 上下文对象。用于获取环境，IOC 容器，ClassLoader 对象
     * @param annotatedTypeMetadata 注解元对象。可以获取注解定义的属性值。
     * @return boolean
     */
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        final Map<String, Object> annotationAttributes = annotatedTypeMetadata.getAnnotationAttributes(ControllerAopMatch.class.getName());
        Optional.ofNullable(annotationAttributes).ifPresent(
                stringObjectMap -> System.out.println("stringObjectMap: " + stringObjectMap)
        );

        Environment environment = conditionContext.getEnvironment();
        return environment.getProperty(controllerAopEnable, Boolean.class, false);
    }
}
